var widget = {};
widget = {
    _UIDEFAULFS: {},
    _rebuildUiDefaults: function (options) {
        widget._UIDEFAULFS = Iptools.Tool._extend(widget._UIDEFAULFS, options);
    },
    _addEventListener: function (options) {
        $(options.container).on(options.type, options.target, options.event);
    },
    _bindingDomEvent: function () {
        widget._enableLogin();
    },
    _bindingEventAfterLoad: function () {
    },
    _init: function () {
        widget._bindingDomEvent(0);
        widget._setLoginForm();
    },
    _setLoginForm: function () {
        $("#loginForm").bootstrapValidator({
            fields: {
                loginName: {
                    validators: {
                        notEmpty: {
                            message: "请输入登录账号"
                        }
                    }
                },
                loginPwd: {
                    validators: {
                        notEmpty: {
                            message: "请输入密码"
                        }
                    }
                }
            }
        });
    },
    _enableLogin: function () {
        widget._addEventListener({
            container: "body",
            target: "#loginForm input",
            type: "keydown",
            event: function (e) {
                e = e || event;
                if (e.keyCode == 13) {
                    $("#loginBtn").click();
                }
            }
        });
        widget._addEventListener({
            container: "body",
            target: "#loginBtn",
            type: "click",
            event: function () {
                $("#loginForm").bootstrapValidator('validate');
                if ($("#loginForm").data("bootstrapValidator").isValid()) {
                    var me = $(this);
                    me.addClass("no-events").button("loading");
                    Iptools.PostJson({
                        url: "system/login",
                        data: {
                            loginName: $("input[name='loginName']").val(),
                            loginPwd: $.md5($("input[name='loginPwd']").val())
                        }
                    }).done(function (r) {
                        if (r && r.retcode == "ok") {
                            window.sessionStorage.setItem("token", r.token);
                            self.location = "dashboard"
                        } else {
                            Iptools.Tool.pAlert({
                                type: "danger",
                                title: "登录失败",
                                content: "用户名或密码错误"
                            });
                            me.removeClass("no-events").button("reset");
                        }
                    }).fail(function () {
                        Iptools.Tool.pAlert({
                            type: "danger",
                            title: "登录失败",
                            content: "服务器异常"
                        })
                        me.removeClass("no-events").button("reset");
                    });
                }
            }
        });
    }
};